<?php
/**
 * Created by PhpStorm.
 * User: wx:wenyi2048
 * Date: 2022/3/19 0019
 * Time: 16:45
 */

namespace app\common\service;


use think\Db;
use think\Exception;

class OrderItemsService extends Service
{
    protected $tableName = 'sms_mk_order_items';

    protected $userAdminId;//店铺管理员id

    protected $store = [];

    public function itemAdd($orderId,$goods){
        $total_fee = 0;
        $num = 0;//商品总个数
        //防止冲突
        $thisDb = $this->db;//clone  $this->db;
        //$this->db->where('sms_orders_id','=',$orderId)->delete();
        //添加商品
        $insert = array();
        foreach ($goods as $gid=>$sum){//这里后期应该考虑事务
            if(!$gid) continue;
            $goods = Db::table('sms_mk_goods')
                ->where('id','=',$gid)
                ->find();
            if(!@$goods['price']) continue;
            if($goods['total']<$sum) throw new Exception($goods['name'].'库存不够，请重新选择');
            Db::table('sms_mk_goods')
                ->where('id','=',$gid)
                ->setDec('total',$sum);
            $insert[] = [
                'sms_orders_id'=>$orderId,
                'sms_goods_id'=>$gid,
                'sum'=>$sum
            ];
            $num += $sum;
            $total_fee += $sum*$goods['price'];
        }
        if($total_fee<=0) throw new Exception('价格错误');
        $thisDb->insertAll($insert);
        return [$num,$total_fee];
    }


    public function itemAddUpdate($orderId,$goods,$num,$total_fee){
        //防止冲突
        $thisDb = $this->db;
        //添加商品
        $insert = array();
        foreach ($goods as $gid=>$sum){//这里后期应该考虑事务
            if(!$gid) continue;
            $goods = Db::table('sms_mk_goods')
                ->where('id','=',$gid)
                ->find();
            if(!@$goods['price']) continue;
            if($goods['total']<$sum) throw new Exception($goods['name'].'库存不够，请重新选择');
            Db::table('sms_mk_goods')//减少库存
                ->where('id','=',$gid)
                ->setDec('total',$sum);
            //已经有更新
            $isItem =  Db::table('sms_mk_order_items')//减少库存
                ->where('sms_orders_id','=',$orderId)
                ->where('sms_goods_id','=',$gid)
                ->find();
            if($isItem){
                Db::table('sms_mk_order_items')//减少库存
                ->where('sms_orders_id','=',$orderId)
                ->where('sms_goods_id','=',$gid)
                ->setInc('sum',$sum);
                //$isItem->setInc('sum',[$sum]);
            }else{
                //否则添加
                $insert[] = [
                    'sms_orders_id'=>$orderId,
                    'sms_goods_id'=>$gid,
                    'sum'=>$sum
                ];
            }
            $num += $sum;
            $total_fee += $sum*$goods['price'];
        }
        if($total_fee<=0) throw new Exception('价格错误');
        if($insert)
            $thisDb->insertAll($insert);
        return [$num,$total_fee];
    }


    public function getGoods($orderId){
        return $this->db->alias('oi')
            ->join('sms_mk_goods g','g.id = oi.sms_goods_id')
            ->where('oi.sms_orders_id','=',$orderId)
            ->field('g.*,oi.sum')
            ->select();
    }
}